﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using System.IO;
using Resources;

public partial class ProductCate : SitePage
{
    //------------------------------------------------
    //VARIABLES
    //------------------------------------------------  
    public Category objCategory;
    public string tableProduct = string.Empty;
    protected int TotalProducts = 0;
    //------------------------------------------------
    //METHODS
    //------------------------------------------------  
    protected void Page_Load(object sender, EventArgs e)
    {
       
        LoadCateInforData();
        if (!Page.IsPostBack)
        {
            ResetAction();
            LoadListProduct();
        }
    }

    //------------------------------------------------
    //METHODS
    //------------------------------------------------
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitData();
    }
    private void ResetAction()
    {
        System.Web.UI.HtmlControls.HtmlForm f = this.Page.Form;
        f.Action = this.Request.RawUrl;
    }
    private void InitData()
    {
        this.pagerTop.PageIndex=this.pagerBottom.PageIndex = 1;
        this.pagerTop.PageSize = this.pagerBottom.PageSize = clsConfigData.DEFAULT_PRODUCT_PAGE_SIZE;      
    }

    private void LoadCateInforData()
    {
        string title = string.Empty;
        string metaKeywords = string.Empty;
        string metaDescription = string.Empty;
        int cate = clsQueryString.GetInt("cateId");
        if (cate > 0)
        {
            busCategory objBus = new busCategory();
            objCategory = objBus.GetByID(cate);

            if (objCategory != null && objCategory.CategoryID > 0)
            {
                if(objCategory.PageTitle==string.Empty)
                    title = objCategory.Name;
                else 
                    title = objCategory.PageTitle;
                metaKeywords = objCategory.MetaKeywords;
                metaDescription = objCategory.MetaDescription;
            }
            else
                Response.Redirect("/product");
        }
        else
        {
            objCategory = new Category();
            title = "Product";
        }
        this.SetMetaData(string.Format(Resource.TITLE, title), metaDescription, metaKeywords);
    }  

    private void LoadListProduct()
    {
        ProductCollection lstProduct = new ProductCollection();
        busProduct objBus = new busProduct();

        //get Condition
        Product objProduct = new Product();
        objProduct.CurrentPage = this.pagerTop.PageIndex;
        objProduct.OrderBy = busProduct.CREATEDDATE;
        objProduct.OrderDirection = "DESC";
        objProduct.PageSize = pagerTop.PageSize;     
        objProduct.Condition = "p.IsActive=1 ";

        //Filter Zone
        if (clsCheck.QueryString("z", enmDataType._Int))
        {
            objProduct.Condition += " and ZoneId=" + clsString.QueryString("z");
        }

        if (objCategory.CategoryID > 0)
        {
            objProduct.Condition += " and pc.CategoryID=" + objCategory.CategoryID;
            lstProduct = objBus.GetListByCategoryID(objProduct);
        }
        else
        {
            objProduct.Condition += " and [dbo].[GetCategoryIDByProductID](p.ProductID) is not null ";
            lstProduct = objBus.GetListAll(objProduct);
        }

        
        TotalProducts = 0;
        if (lstProduct != null && lstProduct.Count > 0)
        {
            TotalProducts = lstProduct.Count;
            dalProduct.DataSource = lstProduct;
            dalProduct.DataBind();
            pagerTop.SetPaging(TotalProducts, objProduct.TotalRecords);
            pagerBottom.SetPaging(TotalProducts, objProduct.TotalRecords);
            pagerTop.Visible = true;
            pagerBottom.Visible = true;
        }
        else 
        {
            pagerTop.Visible = false;
            pagerBottom.Visible = false;
        }
    } 

    protected void dalProduct_ItemDataBound(object sender, DataListItemEventArgs e)
    {       
        DataListItem item = e.Item;
        Product data = new Product();
        if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
        {
            if (data == null)
                return;

            data = (Product)item.DataItem;
            PlaceHolder phdControl = (PlaceHolder)item.FindControl("phdControlProduct");
            UserControl ctrl = new UserControl();
            ucProductItem uc = (ucProductItem)ctrl.LoadControl("~/UserControl/ProductItem.ascx");
            if (uc != null)
            {
                uc.ProductItem = data;
                uc.CategoryID = objCategory.CategoryID;
            }

            phdControl.Controls.Add(uc);
        }
    }
    protected void pagerTop_PageIndexChanging(object obj, CommonTagTime.PagerControl.PageIndexChangeEventArgs e)
    {
        this.pagerTop.PageIndex = this.pagerBottom.PageIndex = e.PageIndex;    
        LoadListProduct();
    }

    protected void pagerTop_PageSizeChanging(object obj, CommonTagTime.PagerControl.PageSizeChangeEventArgs e)
    {
        this.pagerTop.PageIndex = this.pagerBottom.PageIndex = 1;
        this.pagerTop.PageSize = this.pagerBottom.PageSize = e.PageSize;
        LoadListProduct();
    }
}